package pl.edu.pk.adcampaignfacebook.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
//import javax.persistence.criteria.CriteriaBuilder;
//import javax.persistence.criteria.CriteriaQuery;
//import javax.persistence.criteria.Root;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import pl.edu.pk.adcampaignfacebook.domain.Account;
import pl.edu.pk.adcampaignfacebook.domain.AccountRole;
import pl.edu.pk.adcampaignfacebook.exception.UserNotFoundException;

public class AccountDaoImpl implements AccountDao
{
//	@Autowired
//	HibernateTemplate template;
	 
//	@Autowired
//	SessionFactory session;
	
	@PersistenceContext
	EntityManager entityManager;

	public Account findUsername(String username) throws UserNotFoundException
	{
		try
		{
			return (Account) entityManager.createQuery("SELECT a FROM Account a WHERE a.username = '" + username + "'").getSingleResult();
		}
		catch(NoResultException nre)
		{
			throw new UserNotFoundException();
		}
	}

	public void save(Account account)
	{
		entityManager.persist(account);
	}

//	//@Override
//	public Collection<Account> listAccounts(Collection<AccountRole> roles)
//	{
//		
//		List<String> c = new ArrayList<String>(roles.size());
//		for(AccountRole in : roles){
//			c.add(in.getRole());
//		}
//		List<Account> accounts = entityManager.createQuery("SELECT a FROM Account a JOIN a.accountRole r WHERE r.role IN(?1)",Account.class).setParameter(1,c).getResultList(); 
//		return accounts;
//	}
}
